//
//  ByUIFriendsAndRelativesVC.m
//  chequanbao_ios
//
//  Created by mc on 2017/11/22.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIFriendsAndRelativesVC.h"

// cell
#import "ByCellFriendsAndRelatives.h"

// api
#import "ByApiMine.h"

// model
#import "ByModelFriendAndRelative.h"

@interface ByUIFriendsAndRelativesVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) NSInteger preTag;
@property (nonatomic, assign) NSInteger page_index;

@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ByUIFriendsAndRelativesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"亲朋好友";
    [self addLeftBackBarButtonItem];
    [self createViews];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewSafeAreaInsetsDidChange{
    [super viewSafeAreaInsetsDidChange];
    self.headerView.top = self.view.safeAreaInsets.top;
    self.tableView.top = self.headerView.bottom + 20;
    self.tableView.height = self.view.height - 80 - self.view.safeAreaInsets.bottom - self.view.safeAreaInsets.top;
}

#pragma mark - 获取数据
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
//    model.uid = @"35";
    model.type = [NSString stringWithFormat:@"%ld",_preTag - 10 + 1];
    model.page_no = [NSString stringWithFormat:@"%ld",self.page_index];
    [ByApiMine fetchFriendList:model success:^(ByNetBaseResult *result) {
        NSInteger count = [result.data[@"count"] integerValue];
        NSMutableArray *temp = [NSMutableArray array];
        for (ByModelFriendAndRelative *model in result.data[@"list"]) {
            [temp addObject:model];
        }
        if (self.page_index == 1) {
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:[temp copy]];
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.dataArray addObjectsFromArray:[temp copy]];
        }
        [self.tableView reloadData];
        [ByCoreToolAlert hideHud];
        if (self.dataArray.count == count) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - getter
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark - 创建视图
- (void)createViews{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    self.headerView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.headerView];
    
    NSArray *buttonAry = @[@"亲朋",@"好友"];
    for (int i = 0; i < buttonAry.count; i ++) {
        UIButton *button = [[UIButton alloc] init];
        [self.headerView addSubview:button];
        CGFloat width = [buttonAry[i] getSizeOfString:16].width + 40;
        CGFloat x = (SCREEN_WIDTH - width * 2) / 3 * (i + 1) + width * i;
        button.frame = CGRectMake(x, 30, width, 30);
        [button setTitle:buttonAry[i] forState:UIControlStateNormal];
        button.titleLabel.font = FONT(16);
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 30 / 2;
        button.tag = 10 + i;
        if (button.tag == 10) {
            _preTag = 10;
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor colorWithR:26 g:127 b:229]];
        }else{
            [button setTitleColor:[UIColor colorWithR:26 g:127 b:229] forState:UIControlStateNormal];
            button.layer.borderColor = [UIColor colorWithR:26 g:127 b:229].CGColor;
        }
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.frame = CGRectMake(0, self.headerView.bottom + 20, SCREEN_WIDTH, SCREEN_HEIGHT - 60 - 20 - 64);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        _page_index = 1;
        [self fetchData];
    }];
    self.tableView.mj_header = header;
    
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        _page_index += 1;
        [self fetchData];
    }];
    self.tableView.mj_footer = footer;
    
}

- (void)buttonAction:(UIButton *)button{
    if (button.tag == _preTag) {
        return;
    }
    UIButton *preButton = [self.headerView viewWithTag:_preTag];
    [preButton setTitleColor:[UIColor colorWithR:26 g:127 b:229] forState:UIControlStateNormal];
    preButton.layer.borderColor = [UIColor colorWithR:26 g:127 b:229].CGColor;
    preButton.backgroundColor = [UIColor clearColor];
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor colorWithR:26 g:127 b:229]];
    _preTag = button.tag;
    self.page_index = 1;
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellFriendsAndRelatives *cell = [ByCellFriendsAndRelatives cellWithTableView:tableView];
    cell.model = self.dataArray[indexPath.section];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}


@end
